<?php

class Bizprofile_Widgets_Profile_Widget extends Tomato_Widget
{
	protected function _prepareShow()
	{		
		$enterprise_id = 0;
		$user_id = 0;
		$auth = Zend_Auth::getInstance();
		$enterprise = null;
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
		$this->_view->assign('lang', $params['lang']);
		
		if ($auth->hasIdentity()) {
			$user = $auth->getIdentity();
			if($user->enterprise){
				$enterprise_id = $user->enterprise->id;
				$user_id = $user->user_id;
				$enterprise = $user->enterprise;
			}
		}

		if($params['module'] == 'bizprofile' && $params['controller'] == 'Enterprise' && $params['action'] == 'index' && isset($params['enterprise_id'])){
			$enterprise_id = $params['enterprise_id'];
			$enterpriseDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
			$enterpriseDao->setDbConnection($conn);
			$enterprise = $enterpriseDao->getById($enterprise_id);
		}
		
		if($enterprise_id > 0 && $enterprise != null && $enterprise->frontend_active == 1){
			// dem so luong recomment cac doanh nghiep khac viet cho minh`
			$conn = Tomato_Db_Connection::factory()->getMasterConnection();
			$recommentDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPartnerDao();
			$recommentDao->setDbConnection($conn);
			$exp = array(
				'relationid' => $enterprise_id,
				'recomment'  => true 	
			);
			$this->_view->assign('countReComment',$recommentDao->count($exp));
			
			$this->_view->assign('enterprise', $enterprise);

			$enterpriseDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
			$enterpriseDao->setDbConnection($conn);
			$profession = $enterpriseDao->getProfessionByIds($enterprise->professions_id);
			if($profession != null){
				$this->_view->assign('profession', $profession);
			}

			$infoDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getInfoDao();
			$infoDao->setDbConnection($conn);
			$infoDao->setLang($params['lang']);

			$infos = $infoDao->getInfos($enterprise_id, 5);

			//Can remove after to fix no infos
			$count = count($infos);
			if( $count < 5){
				for($i = $count + 1; $i <= 5; $i++ ){
					$info = new Enterprise_Models_Info(array(
						'info_title' 			=> "TAB $i",
						'info_string'			=> "",
						'info_number'			=> $i,
						'enterprise_id'			=> $enterprise_id,
						'language'				=> $params['lang']
					));
					$infoDao->add($info);
				}
				$infos = $infoDao->getInfos($enterprise_id, 5);
			}
			//end

			$this->_view->assign('info', $infos[4]);

			$permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
			$permissionDao->setDbConnection($conn);
			
			/** Khanh
	        * Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
	        * $user_entity = 1 : User chua login
	        * $user_entity = 2 : User da login, nhung ko la DN
	        * $user_entity = 3 : User da login, la DN khach
	        * $user_entity = 4 : User da login, la DN doi tac
	        **/
			if(isset($params['enterprise_id']))
			{
				//$auth = Zend_Auth::getInstance();
		        if ($auth->hasIdentity()) 
		        { 
		            $user = $auth->getIdentity();
		            if(!isset($user->enterprise->id)){
			        	$user_entity = 2; // user da login, nhung ko phai la DN
			        }
			        else
			        {
			        	$partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPartnerDao();
		                $partnerDao->setDbConnection($conn);
		
		                $pageIndex     = 1;
		                $perPage = 5;
		                $offset     = ($pageIndex - 1) * $perPage;
		                $exp = array(
		                    'relationid' => $params['enterprise_id'],
		                    'partner_type' => 'buddy',
		                    'partner_status' => 'yes',
							'deleted'		 => 0
		                );
						
		                //* In ralationship
		                $partners = $partnerDao->find($offset, $perPage, $exp);
		                $numPartners   = $partnerDao->count($exp);
		                
		                $user_entity = 3;// DN khach
		                $request = $this->getRequest();
		                foreach($partners as $row):
							$expRecomment = array(
								'relationid' => $row->relationid,
								'enterprise_id' => $row->enterprise_id, // DN doi tac
							);
							if($user->enterprise->id == $row->enterprise_id)
							{
								$user_entity = 4; // DN doi tac
							}
						endforeach;
			        }
		        }
		        else 
		        {
		        	$user_entity = 1; // user chua login 
		        }
			}
			/**
		`	* END : Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
		    **/
			else 
			{
				$user_entity = $permissionDao->getUserEntity($user_id,$enterprise_id);
			}
			//echo "user_entity = ". $user_entity;
			if ($auth->hasIdentity()) { 				
	            $user = $auth->getIdentity();
				if($user->enterprise){
					if($user->enterprise->id == $enterprise_id) // user đã login thuộc DN đang xem 
			    	{
			    		$permission_viewall = 1;
			    		$this->_view->assign('permission_viewall', $permission_viewall);
			    	}
				}
        	}
			$this->_view->assign('user_entity', $user_entity);

			$permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
			$permissionDao->setDbConnection($conn);
			$permission = $permissionDao->getPermissionByUserEntity($enterprise_id, $user_entity);
			
			if($permission != null){
				$permission->general_info = unserialize($permission->general_info);
				$permission->enterprise_info = unserialize($permission->enterprise_info);
				$this->_view->assign('permission', $permission);
			}
		}
	}

	protected function _prepareConfig()
	{

	}
}
